/**
 * Copyright (c) 2010 Moogene
 * All rights reserved
 *
 * This software is the confidential and proprietary information of Moogene.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into
 * with Moogene
 */
package com.moogene.android.engine.action;

import android.util.Log;
import android.view.MotionEvent;

/**
 * @author Song, jiawei
 *
 */
public class ActionManager {
	private static String TAG = "ActionManager";
	private static ActionManager sActionManager;
	private ActionListener mActionListener;
	private TouchAnalyse mTouchAnalyse;
	private KeyAnalyse mKeyAnalyse;
	private ActTouchEvent mActTouchEvent;
	private ActGestureEvent mActGestureEvent;
	private ActKeyEvent mActKeyEvent;
	private ActionMonitor mActionMonitor;

	private ActionManager() {
		mTouchAnalyse = TouchAnalyse.getInstance();
		mKeyAnalyse = KeyAnalyse.getInstance();
		mActTouchEvent = ActTouchEvent.getInstance();
		mActGestureEvent = ActGestureEvent.getInstance();
		mActionMonitor = ActionMonitorProxy.getInstance();
	}

	public static ActionManager getInstance() {
		if (sActionManager == null) {
			sActionManager = new ActionManager();
		}
		return sActionManager;
	}

	public void setActionListener(ActionListener al) {
		mActionListener = al;
	}

	public void update() {
//		mTouchAnalyse.update();
		mKeyAnalyse.update();
//		int touchAct = mTouchAnalyse.getReadyAction();
//		if(touchAct == ActTouchEvent.TYPE_INVALID){
//		    return;
//		}
//		log("ReadyAction: " + touchAct);
//		mActTouchEvent.setup(touchAct, mTouchAnalyse.mX, mTouchAnalyse.mY);
		MotionEvent me = mTouchAnalyse.getMotionEvent();
		if (me != null) {
			mActTouchEvent.setup(me);
			if (Config.LISTENER_SUPPORT) {
				if (mActionListener != null) {
					if (Config.GESTURE_SUPPORT) {
						if (mActGestureEvent.detectGesture()) {
							// mActionListener.actionPerformed(mActGestureEvent);
							return;
						}
					}
					mActionListener.actionPerformed(mActTouchEvent);
				}
			}
		}
	}

	public TouchAnalyse getTouchAnalyse() {
		return mTouchAnalyse;
	}

	public KeyAnalyse getKeyAnalyse() {
		return mKeyAnalyse;
	}

	public ActionMonitor getActionMonitor() {
		return mActionMonitor;
	}

	static void log(String msg){
		if(Config.LOG_ENABLE){
			Log.d(Config.LOG_TAG + "." + TAG,msg);
		}
	}
}
